import { accountType, ResultCommon, ResultPagination } from "@/utils/commonType";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";

/** 获取系统管理-用户管理列表 */
export const getAccountList = async (data?: object) => {
  const result = await http.request<ResultPagination<accountType>>("get", baseUrlApi("/account/lists"), {
    params: data
  });
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

/**
 *
 * @param data 新增账户信息
 * @returns
 */
export const addAccount = async (data: object) => {
  const result = await http.request<ResultCommon<accountType>>(
    "post",
    baseUrlApi("/account/add"),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

/**
 *
 * @param data 激活账户信息
 * @returns
 */
export const activeAccount = async (data: object) => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/active/" + data),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};


/**
 *
 * @param data 停用账户信息
 * @returns
 */
export const archiveAccount = async (data: object) => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/archive/" + data),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const setAccountType = async data => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/update/" + data.accountName),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const resetAccountPwd = async data => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/resetPwd/" + data.accountName)
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result.data;
};

export const updateAccount = async data => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/update/" + data.accountName),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result.data;
};

export const updateOwnerAccount = async data => {
  const result = await http.request<ResultCommon<accountType>>(
    "put",
    baseUrlApi("/account/update-owner/" + data.accountName),
    { data }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result.data;
};
